package com.gkwave.module.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;



/**
 * @author drengasa
 *
 */
public class BaseDAO {

	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private Statement stmt;
	private Connection con;
	@SuppressWarnings("unused")
	private ResultSet rset;
	@SuppressWarnings("unused")
	private PreparedStatement pstmt;
	private String url ;
	private String username;
	private String password;
	private boolean flag=false;
	public BaseDAO() {
		// TODO Auto-generated constructor stub
		try{
			getConnection();
			if(con==null||con.equals(null))Class.forName("com.mysql.jdbc.Driver").newInstance();
		}catch (ClassNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (IllegalAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (InstantiationException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public Connection openConnection()throws SQLException,Exception{
		try{
			if(con==null)con = DriverManager.getConnection(url,username,password);
			
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return con;		
	}
	
	public boolean closeConnection(Statement stmt,
								 Connection con,
								 ResultSet rset)throws SQLException,Exception{
		try {
			stmt.close();
			con.close();
			rset.close();			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
		
	}
	
	public boolean closeConnection(Statement stmt, Connection con)throws SQLException, Exception {
		try {
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;

	} 
	
	public boolean closeConnection(PreparedStatement pstmt, Connection con)throws SQLException, Exception {
		try {
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;

	}	
	
    /**
     *  Gets a connection from the properties file.
     *  @return the database connection
     */
    public  void getConnection()throws SQLException, IOException{
        Properties props = new Properties();
//        System.out.println(this.getClass().getClassLoader());
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("database.properties");
        props.load(in);
        in.close();
        String drivers = props.getProperty("jdbc.drivers");
        if (drivers != null)
        {
//            System.setProperty("jdbc.drivers", drivers);
        }
        url = props.getProperty("jdbc.url");
        username = props.getProperty("jdbc.username");
        password = props.getProperty("jdbc.password");
    }	
}
